package com.cyt.web.controller.sys;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Month;
import cn.hutool.core.date.Week;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.SysMenuService;
import com.cyt.sys.service.SysUserService;
import com.cyt.sys.util.FilePathUtil;
import com.cyt.wx.model.po.WxApply;
import com.cyt.wx.model.po.WxApplyUse;
import com.cyt.wx.service.WxApplyMenuService;
import com.cyt.wx.service.WxApplyService;
import com.cyt.wx.service.WxApplyUseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;

/**
 * 登录控制层
 *
 * @author YuShengQiang
 * @date 2018-11-17 8:21
 */
@RestController
public class SysLoginController extends SysBaseController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private WxApplyService wxApplyService;
    @Resource
    private WxApplyUseService wxApplyUseService;
    @Resource
    private WxApplyMenuService wxApplyMenuService;

    /**
     * txt文件验证
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/{path}.txt")
    public String checkTxt(HttpServletRequest request,@PathVariable String path){
        StringBuilder result = new StringBuilder();
        // 项目在容器中实际发布运行的根路径
        String realPath = FilePathUtil.acceptCheckRealPath(request);
        String paths=realPath+"/upload/"+path+".txt";
        try{
            BufferedReader br = new BufferedReader(new FileReader(new File(paths)));
            String s = null;
            while((s = br.readLine())!=null){
                result.append(s);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
    /**
     * 统一首页
     *
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/")
    public ModelAndView s(){
        return new ModelAndView("redirect:/web/index");
    }
    /**
     * 基础页面统一跳转处理
     *
     * @author YuShengQiang
     * @date 2018-11-23 9:19
     */
    @RequestMapping("/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 后端登录
     *
     * @author YuShengQiang
     * @date 2018-11-17 8:25
     */
    @CrossOrigin
    @RequestMapping(value = "/webLogin", method = RequestMethod.POST)
    public ReturnResult webLogin(HttpServletRequest request, String loginName, String password) throws Exception {
        return sysUserService.webLogin(request, loginName, password);
    }

    /**
     * 后端退出
     *
     * @author YuShengQiang
     * @date 2018-11-22 9:59
     */
    @RequestMapping(value = "/webExit", method = RequestMethod.GET)
    @BaseLog(methods = "系统模块-用户退出", module = "系统模块")
    public ReturnResult webExit(HttpServletRequest request) {
        return sysUserService.webExit(request);
    }

    /**
     * 后台首页
     *
     * @author YuShengQiang
     * @date 2018-11-22 9:59
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, String applyId) {
        SysUser sysUser = acceptCurrentUser(request);
        ModelAndView modelAndView = new ModelAndView("/index");
        modelAndView.addObject("baseUser", sysUser);
        if (StringUtils.isBlank(applyId)) {
            modelAndView.addObject("iframeContent", "/main");
            modelAndView.addObject("list", sysMenuService.selectByRoleId(DictCode.MENU_TYPE_CATALOG_MENU, sysUser).getData());
        } else {
            modelAndView.addObject("applyId", applyId);
            WxApply wxApply = wxApplyService.selectByPrimaryKey(applyId);
            SessionAddOrUpdateObject(request, BaseConstant.SESSION_APPLY, wxApply);
            WxApplyUse wxApplyUse = wxApplyUseService.selectByPrimaryKey(wxApply.getApplyUseId());
            modelAndView.addObject("applyUseId", wxApplyUse.getId());
            modelAndView.addObject("iframeContent", wxApplyUse.getWebPath());
            modelAndView.addObject("list", wxApplyMenuService.selectByUserId(sysUser.getId()));
        }
        return modelAndView;
    }

    @BaseLog(methods = "系统模块", classz = "日历", module = "日历", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/index/calendar", method = RequestMethod.POST)
    public ReturnResult calendar() {
        DateTime beginOfWeek = DateUtil.beginOfWeek(new Date());

        int year = DateUtil.thisYear();
        String month = Month.of(DateUtil.thisMonth()).name();
        int dayOfWeek = (int) DateUtil.betweenDay(beginOfWeek, new Date(), false);
        List<Map> dayList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("weekname", Week.of((i + 1) % 7 + 1).toString().substring(0, 3));
            map.put("datelist", DateUtil.formatDate(DateUtil.offsetDay(beginOfWeek, i)).substring(8));
            dayList.add(map);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("year", year);
        result.put("month", month);
        result.put("dayOfWeek", dayOfWeek);
        result.put("dayList", dayList);
        return new ReturnResult(result);
    }
}